package com.woniu.service.Impl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.woniu.dao.CreditProductDao;
import com.woniu.entity.CreProduct;
import com.woniu.entity.RepayEntity;
import com.woniu.service.CreditProductService;
import com.woniu.tool.RefundUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@MapperScan("com.woniu.dao")
public class CreditProductServiceImpl implements CreditProductService {
    @Autowired
    private CreditProductDao creditProductDao;

    /*
     * 产品搜索查询
     * */
    @Override
    public List<CreProduct> searchProduct(CreProduct creProduct) {
        List<CreProduct> list = creditProductDao.searchProduct(creProduct);
        return list;
    }

    /*
     * 产品列表查询
     * */
    @Override
    public List<CreProduct> findAllProduct(CreProduct creProduct) {
        return creditProductDao.findAllProduct(creProduct);
    }

    /*
     * 新增产品功能
     * */
    @Override
    public int addProducts(CreProduct creProduct) {
        return creditProductDao.addProducts(creProduct);
    }

    /*
     * 根据id查询产品详情
     * */
    @Override
    public CreProduct findProductsById(int id) {
        return creditProductDao.findProductsById(id);
    }

    /*
     * 修改产品
     * */
    @Override
    public int updateProducts(CreProduct creProduct) {
        return creditProductDao.updateProducts(creProduct);
    }

    /*
     * 物理批量删除
     * */
    @Override
    public int delProducts(int[] ids) {
        return creditProductDao.delProducts(ids);
    }

    /*
     * 状态修改
     * */
    @Override
    public Integer editOneType(String enableStatus, Integer id) {
        return creditProductDao.editOneType(enableStatus, id);
    }

    @Override
    public List<CreProduct> anonymousFindAll(CreProduct creProduct) {
        return creditProductDao.anonymousFindAll(creProduct);
    }

    @Override
    public AjaxResult getMoney(RepayEntity repayEntity) {
        if (repayEntity.getLoanAmount() == null) {
            repayEntity.setLoanAmount(0.0);
        }
        if (repayEntity.getMonthlyRate() == null) {
            repayEntity.setMonthlyRate(0.0);
        }
        ;
        if (repayEntity.getMethod() == null) {
            repayEntity.setMethod("0");
        }
        ;
        if (repayEntity.getConsultingFeeRate() == null) {
            repayEntity.setConsultingFeeRate(0.0);
        }
        ;
        double loanTerm = repayEntity.getLoanTerm();
        Double monthlyRate = repayEntity.getMonthlyRate();
        Double marginRate = repayEntity.getMarginRate();
        Double consultingFeeRate = repayEntity.getConsultingFeeRate();
        Double loanAmount = repayEntity.getLoanAmount();
        String method = repayEntity.getMethod();
        String[] strings = new String[0];
        if (method.equals("0")) {
            /*
            等额本息
            @param loanAmount   贷款金额
     * @param loanTerm          贷款期限
     * @param monthlyRate       月利率
     * @param consultingFeeRate 咨询费利率
     * @param marginRate        保证金利率
             */
            strings = RefundUtil.calculateEqualPrincipalAndInterest(loanAmount, loanTerm, monthlyRate, consultingFeeRate, marginRate);
        } else if (method.equals("1")) {
            /*
            等额本金
     *@param loanAmount        贷款金额
     * @param loanTerm          贷款期限
     * @param monthlyRate       月利率
     * @param consultingFeeRate 咨询费利率
     * @param marginRate        保证金利率
             */
//            calculateEqualPrincipal(1000, 24, 0.49, 0.1, 0.1)
            strings = RefundUtil.calculateEqualPrincipal(loanAmount, (int) loanTerm, monthlyRate, consultingFeeRate, marginRate);
        }
        return AjaxResult.success(strings);
    }
}
